#! /bin/bash
#Entry script for unified Linux project
################################################################################
#  
#
# 通用Linux项目编译通常分为以下几个部分：
#	1. ./do.sh boot compile
#	2. ./do.sh kernel compile
#	3. ./do.sh rootfs compile
#	4. ./do.sh <apps> compile 可能会有多个app
#	5. ./do.sh [all] compile
#		a. ./do.sh <apps> compile
#		b. copy <apps> target bin to rootfs subdirectory
#		c. ./do.sh rootfs compile
#		d. tar image by makeimage tool
#
################################################################################

DIR_CUR=`pwd`

trap '""' 2   #ctrl+c

num_arguments=2
arguments1="boot|dtb|kernel|rootfs|tools|opt|drivers all compile|clean"
#arguments2="one|two|three|all compile|clean"
echo
echo "********************************************************************";
echo "*        Welcome to use Unified Linux Project Builder              *";
echo "********************************************************************";
echo

show_msg()
{
	echo
	echo This is my Project Message.
}

##checking arguments
if [ $# -ne $num_arguments ]
then
	echo -e '\E[31;40mError:Wrong parameter. Please check it.'; tput sgr0;      #30-37,40-47
	echo -e '\E[32;40m';
	echo "Usage 1: $0 $arguments1"
#	echo "Usage 2: $0 $arguments2"
	tput sgr0;  
	show_msg
	exit 
fi

#special treatment for "all"
if [ $1 == "all" ]
then
	if [ $2 == "clean" ]
	then
		rm -rf $DIR_CUR/build/output
		exit 0
	else
		./do.sh boot     $2				    #
		./do.sh dtb      $2				    #
		./do.sh kernel_golden   $2			#
		./do.sh kernel_update   $2			#
		#./do.sh dtb      $2				#
		#./do.sh rootfs   $2
#add other apps make here			
		#编译opt目录
		#./do.sh opt $2		
		exit 0
	fi
fi

##parse arguments, then jump to right exec subdirectory
cd $DIR_CUR/build;
DIR_SUB=`find ./ -maxdepth 1 -iname "*" | grep -E "\/$1$"`
if [ -z "$DIR_SUB" ]
then
	echo NO directory: $1. Please check it!
	exit
fi
cd ${DIR_SUB}
pwd


##.invoke the exec script in subdirectory
if [ -e $1-build.sh ]
then
	cd $DIR_CUR/build;
	if [ -e do-env.sh ]
	then
		./do-env.sh
	fi

	cd - > /dev/null;
	#echo ./$1-build.sh $1 $2;
	./$1-build.sh $1 $2;
	exit 0
else
	echo "Error: no $1-build.sh!" 
	exit
fi

